Tkinter: "NameError: name 'event' is not defined error" when passing argument in button command

您所在的位置:网站首页 tkinter command Tkinter: "NameError: name 'event' is not defined error" when passing argument in button command

Tkinter: "NameError: name 'event' is not defined error" when passing argument in button command

#Tkinter: "NameError: name 'event' is not defined error" when passing argument in button command| 来源: 网络整理| 查看: 265

Button press is not considered an event. An event is binding a function to a mouse, or keyboard action. In this example I get the cursor position upon left click, relative to the top left corner of the screen. The event is bound to the root widget.

import tkinter as tk class MainApplication(tk.Frame): def __init__(self, parent, *args, **kwargs): tk.Frame.__init__(self, parent, *args, **kwargs) frame = tk.Frame(root) frame.pack(fill='x') self.button = tk.Button(frame, text='M', font='Verdana 13 bold', command= lambda: self.toplevel(x, y)) self.button.pack(side='right', anchor='e', padx=10, pady=10) def toplevel(self, x, y): self.toplevel = tk.Toplevel() self.toplevel.overrideredirect(True) self.toplevel.geometry(f"100x130+{x}+{y}") button = tk.Button(self.toplevel, text="Quit") button.pack(padx=5, pady=5, anchor='w') def get_event(event): global x, y x = event.x_root y = event.y_root if __name__ == "__main__": root = tk.Tk() root.title('Title') MainApplication(root).pack(side="top", fill="both", expand=True) root.bind('', get_event) root.mainloop()


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3